<?php

namespace App\Http\Controllers\Home;

use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class SortController extends CommonController
{
    //产品分类页面 产品信息列表
    public function index(Request $request, BaseServer $baseServer)
    {
        $tmpChunk = \App\Chunk::where('path', $request->chunk)
            ->where('status', 1)
            ->get()
            ->toArray();
        $tmpChunk ? $data['chunk'] = $tmpChunk[0] : abort(404);

        $data['infos'] = \App\Info::where('infos.status', 1)
            ->where('info_sort_lines.chunk', $data['chunk']['id'])
            ->where('sorts.status', 1)
            ->whereNull('set_time_out')
            ->where(function ($query) use ($request) {
                if ($request->sort) {
                    $query->where('sorts.id', $request->sort)->Orwhere('sorts.path', 'like', '%,' . $request->sort . ',%');
                }
            })
            ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
            ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
            ->join('sorts', 'sorts.id', '=', 'info_sort_lines.sort')
            ->join('admins', 'admins.id', '=', 'infos.author')
            ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
            ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
            ->select('infos.*', 'sorts.name as sort_name', 'sorts.id as sort_id', 'chunks.name as chunk_name', 'chunks.path', 'images.path as image', 'images.water', 'admins.name as author', 'infos.created_at as time')
            ->orderBy('xindex', 'DESC')
            ->orderBy('created_at', 'DESC')
            ->GroupBy('infos.id')
            ->paginate($data['chunk']['paginate']);

        if (config('set.onepage') && $data['infos']->currentPage() == 1 && count($data['infos']) == 1) {
            $one = $data['infos'][0];
            return redirect()->route('info', [$one['path'], $one['id']]);
        }

        foreach ($data['infos'] as $k => $info) {
            $data['infos'][$k]->image = !empty($info->water) ? $info->water : $info->image;
            if (!empty($info->file)) {
                $data['infos'][$k]->filesize = calc(filesize('.' . $info->file));
            }
        }

        if ($request->sort) {
            $data['sort'] = \App\Sort::leftjoin('images', 'images.id', '=', 'sorts.image')
                ->where('sorts.status', 1)
                ->select('sorts.*', 'images.path as image')
                ->findorfail($request->sort)
                ->toArray();
            $theme = $data['sort']['theme'];

            if (config('area')) {
                $data['title'] = $data['chunk']['name'] . '-' . $data['sort']['name'] . '_' . config('set.web_name') . '_' . config('range')[config('area')] . '分站';
            } else {
                $data['title'] = $data['chunk']['name'] . '-' . $data['sort']['name'] . '_' . config('set.web_name');
            }

            $data['keywords'] = $data['sort']['name'];
            $data['description'] = !empty($data['sort']['simpledescribe']) ?
                $data['sort']['simpledescribe'] :
                config('set.web_description');
            $data['frames'] = $baseServer->frame([
                ['url' => '', 'name' => $data['chunk']['name']],
                ['url' => route('sort_all', $data['chunk']['path']), 'name' => config('app.all_sort_name')],
                ['url' => route('sort', [$data['chunk']['path'], $data['sort']['id']]), 'name' => $data['sort']['name']],
            ]);
            $data['describe'] = $data['sort']['describe'];
        } else {
            $theme = $data['chunk']['sort_theme'];

            if (config('area')) {
                $data['title'] = $data['chunk']['name'] . '_' . config('set.web_name') . '_' . config('range')[config('area')] . '分站';
            } else {
                $data['title'] = $data['chunk']['name'] . '_' . config('set.web_name');
            }

            $data['keywords'] = $data['chunk']['name'];
            $data['description'] = !empty($data['chunk']['simpledescribe']) ?
                $data['chunk']['simpledescribe'] :
                config('set.web_description');
            $data['frames'] = $baseServer->frame([
                ['url' => '', 'name' => $data['chunk']['name']],
                ['url' => route('sort_all', $data['chunk']['path']), 'name' => config('app.all_sort_name')],
            ]);
            $data['describe'] = $data['chunk']['describe'];
        }

        $baseServer->keywordDowork($request, $data['keywords']);
        if ((bool)config('set.wap_jump') && is_mobile()) {
            return view('home.m.' . config('set.wap_theme') . '.sort.' . $theme, $data);
        }

        return view('home.pc.sort.' . $theme, $data);
    }
}
